﻿// ***********************************************************************
// Assembly         : TSharp.Core
// Author           : tangjingbo
// Created          : 05-25-2013
//
// Last Modified By : tangjingbo
// Last Modified On : 05-25-2013
// ***********************************************************************
// <copyright file="LinqGetPropertyExtension.cs" company="Extendsoft">
//     Copyright (c) Extendsoft. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace TSharp.Core
{
    /// <summary>
    /// Class LinqGetPropertyExtension
    /// </summary>
    public static class LinqGetPropertyExtension
    {
        /// <summary>
        /// The list
        /// </summary>
        private static readonly ConcurrentDictionary<Type, Delegate> List = new ConcurrentDictionary<Type, Delegate>();

        /// <summary>
        /// The dateformate
        /// </summary>
        private static readonly string dateformate = "yyyy-MM-dd";

        /// <summary>
        /// 获取指定属性表达式的值，即时当前对象为null，也不会报错
        /// </summary>
        /// <typeparam name="TProperty">The type of the T ret.</typeparam>
        /// <typeparam name="TObject">The type of the T arg.</typeparam>
        /// <param name="arg">当前对象.</param>
        /// <param name="expr">取值表达式.</param>
        /// <returns>``0.</returns>
        /// <exception cref="System.NullReferenceException"></exception>
        public static TProperty GetProperty<TProperty, TObject>(this TObject arg, Func<TObject, TProperty> expr) where TObject : class
        {
            if (arg == null)
                return default(TProperty);
            return expr(arg);

            //var func = List.GetOrAdd(expr.Type,
            //    key => expr.Compile()

            //    ) as Func<TObject, TProperty>;
            //if (func != null)
            //    return func(arg);
            //throw new NullReferenceException(string.Format("从缓存取到的引用为null。缓存Key:'{0}'", expr.Type.FullName));
        }

        /// <summary>
        /// To the format string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns>System.String.</returns>
        public static string ToFormatString(this DateTime date)
        {
            return date.ToString(dateformate);
        }

        /// <summary>
        /// To the format string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <param name="format">The format.</param>
        /// <returns>System.String.</returns>
        public static string ToFormatString(this DateTime date, string format)
        {
            return date.ToString(format);
        }

        /// <summary>
        /// To the format string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns>System.String.</returns>
        public static string ToFormatString(this DateTime? date)
        {
            if (date.HasValue)
                return date.Value.ToString(dateformate);
            return null;
        }

        /// <summary>
        /// To the format string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <param name="format">The format.</param>
        /// <returns>System.String.</returns>
        public static string ToFormatString(this DateTime? date, string format)
        {
            if (date.HasValue)
                return date.Value.ToString(format);
            return null;
        }
    }
}
